<?php
require 'picks_model.php';

class Admin_Model extends Picks_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Return list with possible status for round
     * @return type PDO-statement. 
     * data = returnvalue->fetch(); 
     * output = data['columnname'];
     */
    function GetPosibleRoundStatus() {
        $sth = $this->database->prepare("SELECT status FROM `PossibleRoundStatus`");
        $sth->execute();
        return $sth;
    }
    
    /**
     * Update Round-status for a given round nr.
     * @param type $RoundNr
     * @param type $RoundStatus 
     */
    function UpdateRoundStatus($RoundNr, $RoundStatus) {
        $sthUpdate = $this->database->prepare("UPDATE Rounds SET roundStatus=:RoundStatus WHERE nr=:nr");            
        $sthUpdate->execute(array(
            ':RoundStatus' => $RoundStatus,
            ':nr' => $RoundNr,
        ));
    }

    /**
     * Return list with possible result for a Match
     * @return type PDO-statement. 
     * data = returnvalue->fetch(); 
     * output = data['columnname'];
     */
    function GetPosibleMatchResult() {
        $sth = $this->database->prepare("SELECT result FROM `PossibleMatchResults`");
        $sth->execute();
        return $sth;
    }
    
    
    /**
     * Return list with matchId, nameTeamA, nameTeamB, round for Matchs
     * @return type PDO-statement. 
     * data = returnvalue->fetch(); 
     * output = data['columnname'];
     */
    function GetMatchs() {
        $sth = $this->database->prepare("SELECT matchId, nameTeamA, nameTeamB, round FROM Matchs ORDER BY matchId ASC");
        $sth->execute();
        return $sth;
    }
    
    function UpdateMatchResult($MatchNr, $MatchResult) {
        $sthUpdate = $this->database->prepare("UPDATE Matchs SET matchResult=:MatchResult WHERE matchId=:MatchNr");            
        $sthUpdate->execute(array(
            ':MatchResult' => $MatchResult,
            ':MatchNr' => $MatchNr,
        ));
        
    }
    
    function UpdateScore() {
    	
        //For all users
        $sthMembers = $this->database->prepare("SELECT memberId FROM Members");
        $sthMembers->execute();
        
        while($data = $sthMembers->fetch()) {
            $TotalPoints = 0;
            $TotalWinningPicks = 0;
            $TotalWinningFirstPicks = 0;
            $memberId = $data["memberId"];
            
            //Get members played picks where MatchResult is different from 'not played', return pick-result, match-result and point
            $sthPlayedMatchs = $this->database->prepare("SELECT Picks.picksResult, Picks.priority, Matchs.matchResult, PriorityPoints.points  FROM `Picks` INNER JOIN PriorityPoints ON Picks.priority=PriorityPoints.priority INNER JOIN Matchs ON Picks.matchId=Matchs.matchId WHERE Picks.memberId=".$memberId." AND Picks.picksResult <> 'Not Played' AND Matchs.matchResult<>'Not Played'");
            $sthPlayedMatchs->execute();
            
            //Loop through matchs where Picks is set and Match is played
            while($Matchdata = $sthPlayedMatchs->fetch()) {
                $MatchResult = $Matchdata['matchResult'];
                $PickResult = $Matchdata['picksResult'];
                $Points = $Matchdata['points'];
                $Priority = $Matchdata['priority'];
                
                //Update Score if match result is tie or Matchresult and picks result are the same
                if(strcmp($MatchResult, 'Tie')==0 || strcmp($MatchResult,$PickResult) == 0) {
                    $TotalPoints = $TotalPoints + $Points;
                    $TotalWinningPicks = $TotalWinningPicks+1;
                    if($Priority == 1)
                        $TotalWinningFirstPicks = $TotalWinningFirstPicks+1;
                    
                }                        
                
            }

            //Update users scoretable
            $sthUpdateScore = $this->database->prepare("UPDATE Members SET score=".$TotalPoints." , correctPicks=".$TotalWinningPicks.", correctFirstPicks=".$TotalWinningFirstPicks." WHERE memberId=".$memberId);
            $sthUpdateScore->execute();
        }
    }
}